package com.example.day06;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {
    private View view1, view2, view3;
    private ViewPager viewPager;  //对应的viewPager
    private List<View> viewList;//view数组

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        viewPager =(ViewPager) findViewById(R.id.viewpage);
        view1 = getLayoutInflater().inflate(R.layout.main1,null);
        view2 = getLayoutInflater().inflate(R.layout.main2,null);
        view3 = getLayoutInflater().inflate(R.layout.main3,null);
        viewList = new ArrayList<>();
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
       //
        PagerAdapter pagerAdapter = new PagerAdapter() {
            /**
             * 判断是否page view与 instantiateItem(ViewGroup, int)返回的object的key 是否相同，
             * 以提供给其他的函数使用
             * @param arg0
             * @param arg1
             * @return
             */
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                //返回有效的View的个数
                return viewList.size();
            }

            /**
             * 该方法的功能是移除一个给定位置的页面。
             * @param container
             * @param position
             * @param object
             */
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }


            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }

        };
        viewPager.setAdapter(pagerAdapter);
    }
}